/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unilearn.dao.hibernate;

import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import unilearn.dao.AssignmentDAO;
import unilearn.vo.Assignment;

/**
 *
 * @author Yat
 */
public class AssignmentDAOImpl extends HibernateDAO implements AssignmentDAO {

    public AssignmentDAOImpl() {
        super();
    }

    public AssignmentDAOImpl(SessionFactory sessionFactory) {
        super(sessionFactory);
    }

    @Override
    public void setSessionFactory(SessionFactory sessionFactory) {
        super.setSessionFactory(sessionFactory);
    }

    @Override
    public List<Assignment> getAll() throws Exception {
        return super.getAll(Assignment.class.getName());
    }

    @Override
    public Assignment get(int id) throws Exception {
        Session s = sessionFactory.openSession();
        Transaction t = s.beginTransaction();

        try {
            Query query = s.createQuery("from " + Assignment.class.getName() + " a where a.id = " + id);
            List<Assignment> as = query.list();
            if ((as == null) || (as.isEmpty())) {
                return null;
            }

            return as.get(0);
        } finally {
            t.commit();
            s.close();
        }
    }

    @Override
    public List<Assignment> find(Assignment assignment) throws Exception {
        return (List<Assignment>) super.find(assignment);
    }

    @Override
    public Assignment insert(Assignment assignment) throws Exception {
        super.insert(assignment);
        return assignment;
    }

    @Override
    public Assignment update(Assignment assignment) throws Exception {
        super.update(assignment);
        return assignment;
    }

    @Override
    public void delete(Assignment assignment) throws Exception {
        super.delete(assignment);
    }
}
